﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Collections.Generic;
using System.IO;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Preview;
using DevExpress.XtraReports.UserDesigner;
using DevExpress.XtraSplashScreen;
using XCI.Extensions;
using XCI.Sys;
using XCI.Sys.Controls;
using XCI.Sys.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Helper;

namespace XCI.Sys.Helper
{
    public static class ReportHelper
    {
        public static void CreateDocument(XtraReportx report, DocumentViewer documentViewer)
        {
            documentViewer.PrintingSystem = report.PrintingSystem;
            report.CreateDocument(true);
        }
        private static IOverlaySplashScreenHandle ShowProgressPanel()
        {
            if (SysRuntime.MainForm != null)
            {
                return SplashScreenManager.ShowOverlayForm(SysRuntime.MainForm);
            }
            return null;
        }
        private static void CloseProgressPanel(IOverlaySplashScreenHandle handle)
        {
            if (handle != null)
                SplashScreenManager.CloseOverlayForm(handle);
        }
        public static void Preview(XtraReportx report, string title)
        {
            var handle = ShowProgressPanel();
            var frmPreview = new FrmReportPreview();
            frmPreview.Text = title;
            frmPreview.CreateDocument(report);
            CloseProgressPanel(handle);
            frmPreview.ShowDialog();
            frmPreview.Dispose();
        }

        public static void Design(XtraReportx report, string reportPath)
        {
            var handle = ShowProgressPanel();
            report.PrintingSystem.ExecCommand(PrintingSystemCommand.StopPageBuilding);
            XRDesignRibbonFormEx frmDesign = new XRDesignRibbonFormEx();
            frmDesign.DesignPanel.FileName = reportPath;
            frmDesign.DesignRibbonController.RibbonControl.RibbonStyle = RibbonControlStyle.Office2007;
            frmDesign.DesignRibbonController.RibbonControl.ToolbarLocation = RibbonQuickAccessToolbarLocation.Hidden;
            frmDesign.DesignPanel.SetCommandVisibility(new[]
            {
                ReportCommand.NewReport,
                ReportCommand.NewReportWizard,
                ReportCommand.SaveAll,
                ReportCommand.ShowHTMLViewTab,
                ReportCommand.Close
            }, DevExpress.XtraReports.UserDesigner.CommandVisibility.None);

            var pages = frmDesign.DesignRibbonController.RibbonControl.Pages;
            if (pages.Count == 3) pages[2].Visible = false;

            frmDesign.OpenReport(report);
            CloseProgressPanel(handle);
            frmDesign.ShowDialog();
            if (frmDesign.DesignPanel != null && !frmDesign.DesignPanel.IsDisposed
                                              && frmDesign.DesignPanel.FileName != reportPath
                                              && File.Exists(frmDesign.DesignPanel.FileName))
            {
                File.Copy(frmDesign.DesignPanel.FileName, reportPath, true);
            }
            frmDesign.Dispose();
        }

        public static SysReport GetSysReportByParamData(IDictionary<string, string> paramData)
        {
            if (!paramData.ContainsKey("code") || paramData["code"].IsEmpty())
            {
                MessageBoxHelper.ShowError("请设置报表编码参数,参数名:code");
                return null;
            }

            var code = paramData.Get("code");
            var sysReport = ReportService.Instance.SelectByCode(code);
            if (sysReport == null)
            {
                MessageBoxHelper.ShowError($"无效的报表编码,参数名:code,参数值:{code}");
                return null;
            }

            return sysReport;
        }
    }
}